package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.CartBean;
import com.project.bean.DetailBean;
import com.project.bean.OrderBean;
import com.project.mapper.ICartMapper;
import com.project.mapper.IDetailMapper;
import com.project.mapper.IOrderMapper;
import com.project.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private IOrderMapper orderMapper;
    @Autowired
    private ICartMapper cartMapper;
    @Autowired
    private IDetailMapper detailMapper;

    @Override
    public void add(OrderBean order) {
        //计算购物车总价
        Integer total = cartMapper.calculateTotal(order.getUserId());
        order.setTotal(total);
        //添加订单
        orderMapper.insert(order);
        //查询登陆用户购物车集合
        QueryWrapper<CartBean> qw = new QueryWrapper<>();
        qw.eq("fk_userId", order.getUserId());
        List<CartBean> cartList = cartMapper.selectList(qw);
        //批量添加订单明细
        detailMapper.addMore(order.getId(), cartList);
        //清空购物车
        cartMapper.delete(qw);

    }

    @Override
    public IPage<OrderBean> cutByItem(Integer pageNO, Integer userId, String status, LocalDate startDate, LocalDate endDate) {
        QueryWrapper<OrderBean> qw = new QueryWrapper<>();
        if (userId != null) {
            qw.eq("fk_userId", userId);
        }
        if (status != null && !"".equals(status)) {
            qw.eq("o_status", status);
        }
        if (startDate!=null){
            qw.ge("o_date",startDate);
        }
        if (endDate!=null){
            qw.le("o_date",endDate);
        }

        return orderMapper.selectPage(new Page<>(pageNO,4),qw);
    }

    @Override
    public List<OrderBean> findByStatus() {
        QueryWrapper<OrderBean> qw = new QueryWrapper<>();
        qw.eq("o_status","发货中");
        return orderMapper.selectList(qw);
    }

    @Override
    public OrderBean findById(Integer id) {
        OrderBean order = orderMapper.selectById(id);
        List<DetailBean> detailList = detailMapper.findByOrder(id);
        order.setDetailList(detailList);

        return order;
    }

    @Override
    public void send(Integer id) {
        OrderBean order = orderMapper.selectById(id);
        if ("未发货".equals(order.getStatus())){
            order.setStatus("发货中");
            orderMapper.updateById(order);
        }
    }

    @Override
    public void complete(Integer id) {
        OrderBean order = orderMapper.selectById(id);
        if ("发货中".equals(order.getStatus())){
            order.setStatus("已完结");
            orderMapper.updateById(order);
        }

    }
}
